package com.yg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yg.config.result.R;
import com.yg.config.result.ResultCode;
import com.yg.entity.readAndWriteTemplates.TeacherEducationPromotionTemplate;
import com.yg.entity.singleEntity.Teacher;
import com.yg.entity.singleEntity.TeacherEducationPromotion;
import com.yg.service.TeacherEducationPromotionService;
import com.yg.service.TeacherService;
import com.yg.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 对应sheet02，此表为教师学历提升表
 * 注意！此表的id和教师的id是一个id，同时教师在进行学历提升后，要更改教师的最高学历，即更改教师学历表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@Api("对应sheet02，此表为教师学历提升表")
@RestController
@RequestMapping("/teacherEducationPromotion")
@CrossOrigin
@Slf4j
public class TeacherEducationPromotionController {

    @Autowired
    private TeacherEducationPromotionService teacherEducationPromotionService;

    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有教师学历提升情况
     *
     * @param current 当前页
     * @param limit   每页显示的条目
     * @param request
     * @return
     */
    @ApiOperation("查询所有教师学历提升情况")
    @GetMapping("/getAllTeacherPromotion")
    public R getTeacherPromotion(
            @ApiParam(name = "current", value = "当前页", required = false) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = false) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "HttpRequest", value = "请求头", required = true) HttpServletRequest request
    ) {
        List<TeacherEducationPromotionTemplate> list = null;
        String vip = JwtUtils.getVipByJwtToken(request);

        return teacherEducationPromotionService.getTeacherPromotion((current - 1) * limit, limit, null, vip);
    }

    /**
     * 教师学历提升情况带条件查询
     *
     * @param current
     * @param limit
     * @param promotionTemplate
     * @param request
     * @return
     */
    @ApiOperation("教师学历提升情况带条件查询")
    @PostMapping("/selectTeacherPromotionByCondition")
    public R getTeacherPromotion(
            @ApiParam(name = "current", value = "当前页", required = false) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = false) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "promotionTemplate", value = "教师的查询条件", required = false) @RequestBody(required = false) TeacherEducationPromotionTemplate promotionTemplate,
            @ApiParam(name = "HttpRequest", value = "请求头", required = true) HttpServletRequest request
    ) {

        List<TeacherEducationPromotionTemplate> list = null;
        String vip = JwtUtils.getVipByJwtToken(request);

        return teacherEducationPromotionService.getTeacherPromotion((current - 1) * limit, limit, promotionTemplate, vip);
    }


    /**
     * 修改教师学历提升
     *
     * @param teacherEducationPromotion
     * @return
     */
    @ApiOperation("修改教师学历提升")
    @PutMapping("/putTeacherPromotion")
    public R updateTeacherPromotion(
            @ApiParam(name = "教师学历提升表", value = "教师学历提升数据", required = true)
            @RequestBody TeacherEducationPromotion teacherEducationPromotion
    ) {

        try {
            teacherEducationPromotionService.updateById(teacherEducationPromotion);
            return R.OK().message("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().code(ResultCode.MODIFY_THE_FAILURE).message("修改失败");
        }

    }


    /**
     * 根据学历提升表的ID直接删除
     *
     * @param promotionTemplate id
     * @return
     */
    @ApiOperation("根据学历提升表的ID直接删除")
    @DeleteMapping("/deleteTeacherPromotion")
    public R deleteTeacherPromotion(@ApiParam(name = "教师学历提升表", value = "教师学历提升表", required = true) @RequestBody TeacherEducationPromotionTemplate promotionTemplate) {

        String id = promotionTemplate.getId();

        if (id.equals(null)) {
            return R.ERROR().message("教师学历指定错误");
        } else {
            try {
                teacherEducationPromotionService.removeById(id);
                return R.OK().message("删除成功");
            } catch (Exception e) {
                e.printStackTrace();
                return R.ERROR().code(ResultCode.DELETE_THE_FAILURE).message("删除失败");
            }
        }
    }

    /**
     * 学历提升表的插入
     *
     * @param promotionTemplate 学历提升字段
     * @return
     */
    @ApiOperation("学历提升表的插入")
    @PostMapping("/postTeacherPromotion")
    public R addTeacherPromotion(
            @ApiParam(name = "教师学历提升表", value = "教师学历提升表", required = true) @RequestBody TeacherEducationPromotionTemplate promotionTemplate
    ) {
        Teacher one = null;
        try {
            String teacherId = promotionTemplate.getTeacherId();
            String name = promotionTemplate.getName();

            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
            wrapper.eq("teacher_id", teacherId);

            wrapper.eq("name", name);
            one = teacherService.getOne(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询信息错误");
        }
        if (one != null) {
            TeacherEducationPromotion educationPromotion = new TeacherEducationPromotion();
            BeanUtils.copyProperties(promotionTemplate, educationPromotion);
            teacherEducationPromotionService.save(educationPromotion);
            return R.OK();
        } else {
            return R.ERROR().message("此教师信息查询错误");
        }
    }


    /**
     * 从表格读取，写入数据库
     *
     * @param file
     * @return
     */
    @ApiOperation("从表格读取，写入数据库")
    @PostMapping("/readExcel")
    public R readExcel(
            @ApiParam(name = "file", value = "要上传的文件表格", required = true) MultipartFile file
    ) {

        R r = R.OK();

        teacherEducationPromotionService.readExcel(file, r);

        return r;
    }


    /**
     * 从数据库查询，写出到表格
     */
    @ApiOperation("从数据库查询，写出到表格")
    @GetMapping("/writeExcel")
    public void writeExcel(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "completion", required = false) String completion,
            HttpServletResponse response
    ) {
        TeacherEducationPromotionTemplate promotionTemplate = new TeacherEducationPromotionTemplate();
        promotionTemplate.setName(name);
        promotionTemplate.setCompletion(completion);

        System.out.println("promotionTemplate" + promotionTemplate);
        teacherEducationPromotionService.writeExcel(promotionTemplate, response);
    }
}